//
//  DatabaseHandle.m
//  Douban
//
//  Created by lanou3g on 15/5/20.
//  Copyright (c) 2015年 lanou3g 蓝鸥科技. All rights reserved.
//

#import "DatabaseHandle.h"
#import <sqlite3.h>
@implementation DatabaseHandle

static DatabaseHandle * handle = nil;

+ (DatabaseHandle *)shareInstance
{
    if (handle == nil) {
        handle = [[DatabaseHandle alloc] init];
        [handle openDB];
    }
    return handle;
}


static sqlite3 * petTown = nil; 
//打开数据库
- (void)openDB
{
    if (nil != petTown) {
        return;
    }
    
    //数据库存储在cache文件夹下
    NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString * filePath = [path stringByAppendingPathComponent:@"PetTown.sqlite"];
  
    //打开数据库
    int result = sqlite3_open([filePath UTF8String], &petTown);
    if (result == SQLITE_OK) {
        
        NSString * createActivitySql = @"CREATE TABLE ActivityList (title TEXT,  data BLOB)";
        //执行sql语句
        sqlite3_exec(petTown, [createActivitySql UTF8String], NULL, NULL, NULL);
        
        
    }
}

//关闭数据库
- (void)closeDB
{
    int resilt = sqlite3_close(petTown);
    if (resilt == SQLITE_OK ) {
        petTown = nil;
    }
    
}

//插入收藏的活动
- (void)insertNewActivity:(NSData *)data Key:(NSString *)key
{
    [self openDB];
    
    sqlite3_stmt * stmt = nil;
    NSString * sql = @"insert into ActivityList (title,data) values (?,?)";
    int result = sqlite3_prepare_v2(petTown, [sql UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        
        //绑定数据
        sqlite3_bind_text(stmt, 1, [key UTF8String], -1, nil);
        
        
        sqlite3_bind_blob(stmt, 2, [data bytes], (int)[data length], nil);
        sqlite3_step(stmt);
    }
    sqlite3_finalize(stmt);
}

//获取某个收藏的活动对象
- (NSData *)selectActivityWithTitle:(NSString *)title
{
    
    [self openDB];
    
    sqlite3_stmt * stmt = nil;
    
    NSString * sql = @"select data from ActivityList where title = ?";
    
    int result = sqlite3_prepare_v2(petTown, [sql UTF8String], -1, &stmt, NULL);
    
    NSData * data = nil;
    
    if (result == SQLITE_OK) {
        
        sqlite3_bind_text(stmt, 1, [title UTF8String], -1, NULL);
        
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            
            //bytes length
            
            data = [NSData dataWithBytes:sqlite3_column_blob(stmt, 0) length:sqlite3_column_bytes(stmt, 0)];
            
        }
        
    }
    
    sqlite3_finalize(stmt);
    
    return data;
}

//删除收藏
- (void)deleteStudentByKey:(NSString *)key{
    
    [self openDB];
    
    sqlite3_stmt * stmt = nil;
    NSString * sql = @"delete from ActivityList where title = ?";
    //验证sql语句
    int result=sqlite3_prepare_v2(petTown, [sql UTF8String], -1, &stmt, nil);
    
    if (result==SQLITE_OK) {
        
        //绑定数据
        sqlite3_bind_text(stmt, 1, [key UTF8String], -1, nil);
        
        sqlite3_step(stmt);
    }
}


//判断活动是否被收藏
- (BOOL)isFavoriteActivityWithTitle:(NSString *)title
{
    NSData * data = [self selectActivityWithTitle:title];
    if (data == nil) {
        return NO;
    }
    
    return YES;
    
}

@end
